package br.com.it4.zmed.view.backbean;

import org.apache.myfaces.custom.datascroller.HtmlDataScroller;

import br.com.it4.framework.util.resource.MessageResource;
import br.com.it4.framework.util.resource.MessageResourceFactory;
import br.com.it4.framework.view.backbean.BaseBean;
import br.com.it4.zmed.view.util.ZMedConstants;

public abstract class ZMedBean extends BaseBean {

    protected MessageResource configResource;

    protected MessageResource messageResource;

    public ZMedBean() {
        this.configResource = MessageResourceFactory.createMessageResource(ZMedConstants.RESOURCE_BUNDLE_CONFIG,
                getClass().getClassLoader());
        this.messageResource = MessageResourceFactory.createMessageResource(ZMedConstants.RESOURCE_BUNDLE_MESSAGE,
                getClass().getClassLoader());
    }

    protected String getConfig(String key) {
        return this.configResource.get(key);
    }

    protected String getMessage(String key, Object... params) {
        return this.messageResource.get(key, params);
    }

    public Integer getPageSize() {
        return Integer.valueOf(this.getConfig(ZMedConstants.CONFIG_PAGE_SIZE));
    }

    public Integer getPaginatorMaxPages() {
        return Integer.valueOf(this.getConfig(ZMedConstants.CONFIG_PAGINATOR_MAX_PAGES));
    }

    /**
     * @param page
     *            A página atual.
     * @param pageSize
     *            O número de registros da pagian
     * @return
     */
    public int firstRow(int page, int pageSize) {
        return (page * pageSize) - pageSize;
    }
    
    public void resetFirstPage(String componentId){
        ((HtmlDataScroller)getFacesContext().getViewRoot().findComponent(componentId)).getUIData().setFirst(0);
    }

}
